window.name = "peruser";

function refasehPre(){
    tableIn.reload("PerUsers");
}

var tableIn = null;

function rest(){
    refasehPre();
}

layui.use(['table','form','laydate','upload'], function() {
    var table = layui.table;
    var form = layui.form;
    var laydate = layui.laydate;
    var upload = layui.upload;


    laydate.render({
        elem:'#applyTime',
        type: 'date'
    })
    // 第一个实例
    tableIn = table.render({
        elem: '#demo'
        , url: '/Primary/getAudit?applyState='+'未审核' //数据接口
        , id: 'PerUsers'
        , page: true //开启分页
        , page: {
            layout: ['count', 'prev', 'page', 'next', 'limit'],//展示的按钮
            groups: 5,//展示的联想的页码数
            limit: 10,//每页的条数
            limits: [10, 20],//可选的每页条数
        }, cols: [
            [ //表头
                {type: 'checkbox', fixed: 'left', width: '80'},
                {field: 'applyId', title: 'ID', width: '80', sort: true, fixed: 'left', align: 'center'},
                {field: 'applyName', title: '用户姓名', align: 'center',width: '130'},
                {field: 'applyAccount', title: '用户账号', align: 'center',width: '130'},
                {field: 'applyUnit', title: '申请单位', align: 'center',width: '130'},
                {field: 'applyPhone', title: '电话', align: 'center',width: '130'},
                {field: 'applyTime', title: '申请时间', align: 'center',width: '130'},
                {field: 'applyEmail', title: '邮箱', align: 'center',width: '130'},
                {field: 'applyState', title: '申请状态', align: 'center',width: '130'},
                {field: 'cz', title: '操作', toolbar: '#barDemo', align: 'center',width: '130'}
            ]
        ], parseData: function (res) {
            return {
                "code":res.state,//解析接口状态
                "msg":res.message,//解析提交文本
                "count":res.total,//解析数据长度
                "data":res.data//解析数据列表
            };
        }
        , toolbar: '#toolDemo'
    });

    table.on('tool(test)', function (obj) {//注：tool是工具条时间名，test是table
        console.log(obj.data.applyId);
        console.log(obj.data);
        var e = obj.event;

        if (e === 'cat') {
            // layer.msg("查看"+data.id+data.username);
            layer.open({
                type: 2,
                title: '查看',
                content: 'Cat.html',
                area: ['70%', '70%'],
                id: 'PerUsers',
                success: function (layero, index) {//弹出层成功弹出后的回调函数
                    // 父页面向子页面传参 -- iframe 层传数据
                    var body = layer.getChildFrame('body', index);//获取iframe页面
                    body.find('#applyName').val(obj.data.applyName);
                    body.find('#applyAccount').val(obj.data.applyAccount);
                    body.find('#applyUnit').val(obj.data.applyUnit);
                    body.find('#applyPhone').val(obj.data.applyPhone);
                    body.find('#applyTime').val(obj.data.applyTime);
                    body.find('#applyEmail').val(obj.data.applyEmail);
                    body.find('#applyState').val(obj.data.applyState);
                }
            })
        }
    });

    //获取多选中的数据，获取的是数组，先转换为json字符串在进行前端给后端赋值
    table.on('toolbar(test)', function (obj) {

        var checkStatus = table.checkStatus(obj.config.id);
        console.log(checkStatus);
        console.log('--------------');
        console.log(checkStatus.data[0].applyId);
        console.log('--------------');

        var aa=[];
        for (var a=0;a<checkStatus.data.length;a++){
            aa.push(checkStatus.data[a].applyId+"");
        }
        console.log(aa);
        var e = obj.event;

        if (e === 'audit') {
            $.ajax({
                type:'POST',
                url:'/Primary/audit',
                id:'PerUsers',
                contentType : "application/json" ,
                data: JSON.stringify(aa),
                success:function (){
                    layer.msg("未审核");
                    refasehPre();
                }
            })
        }else if (e === 'through'){
            $.ajax({
                type:'POST',
                url:'/Primary/through',
                id:'PerUsers',
                contentType : "application/json" ,
                data: JSON.stringify(aa),
                success:function (){
                    layer.msg("已批准");
                    refasehPre();
                    if(top.window['RecUsers']){
                        top.window['RecUsers'].frames.location.reload();
                    }
                }
            })
        }else if (e === 'rejected'){
            $.ajax({
                type:'POST',
                dataType:'json',
                contentType : "application/json" ,
                url:'/Primary/rejected',
                id:'PerUsers',
                data: JSON.stringify(aa),
                success:function (){
                    layer.msg("已驳回");
                    refasehPre();
                    if(top.window['upload']){
                        top.window['upload'].frames.location.reload();
                    }
                }
            })
        }
    });


    form.on('submit(go)', function (d) {
        d = d.field;
        console.log(d);
        table.reload('PerUsers', {
            where: {
                applyName: d.applyName,
                applyAccount: d.applyAccount,
                applyTime: d.applyTime
            }, page: {
                page: 1,
                limit: 3
            }
        })
    });

})